﻿using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class StudentRepository : IStudentRepository
    {
        private readonly MyDbContext myDbContext;

        public StudentRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

		public async Task<int> AddStudentAsync(Student student)
		{
			myDbContext.Student.Add(student);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> DeleteStudentAsync(int Id)
		{
			var student = await myDbContext.Student.FindAsync(Id);
			if (student != null)
			{
				myDbContext.Student.Remove(student);
				return await myDbContext.SaveChangesAsync();
			}
			return 0;
		}

		public List<Student> SelectStudentsAsync()
		{
			List<Student> result = new List<Student>();
			result = myDbContext.Student.ToList();
			return result;
		}
		public async Task<int> UpdateStudentAsync(Student student)
		{
			// 将学生实体标记为修改状态
			myDbContext.Student.Update(student);

			// 保存更改到数据库，并返回受影响的行数
			int result = await myDbContext.SaveChangesAsync();

			return result;
		}
	}
}
